﻿using System;
using System.Collections.Generic;
using System.Text;
using SH;

namespace MTS
{
    class mtRunEnv
    {
        //状态
       const uint MTS_ST_UNINITIALIZE = 0x00000001;
        const uint MTS_ST_RUNNING = 0x00000002;
        const uint MTS_ST_SHUTDOWNING = 0x00000003;
        const uint MTS_ST_FAULT = 0x00000004;
        const uint MTS_ST_DIAGNOSIS = 0x00000005;
        const uint MTS_ST_SUSSPENDED = 0x00000006;

        const uint MTS_ST_DISCONNECT = 0x00000010;
        const uint MTS_ST_CONNECTING = 0x00000020;
        const uint MTS_ST_CONNECTED = 0x00000030;

        const uint MTS_ST_IDLE = 0x00000100;
        const uint MTS_ST_NORMAL = 0x00000200;
        const uint MTS_ST_BUSY = 0x00000300;
        const uint MTS_ST_VERYBUSY = 0x00000400;

        const uint MTS_ST_RUNSTATUS_MASK = 0x0000000f;
        const uint MTS_ST_LINKSTATUS_MASK = 0x000000f0;
        const uint MTS_ST_LOADSTATUS_MASK = 0x00000f00;

        public uint m_status;

        public long m_runStatusCode = 0x00;

        mtGStationRunStatusInfo m_localGStationRunStatusInfo;

        mtGSystemStatus m_GSystemStatus;

        //mtGWFTaskProcessInfo m_TaskProcessInfo;

        mtGWFTask m_GWFTask;

        mtRegisterInfo m_info;

        mtCredential m_crd;


        uint GET_LINK_STATUS(uint S) { return S & MTS_ST_LINKSTATUS_MASK; }
        uint GET_LOAD_STATUS(uint S) { return S & MTS_ST_LOADSTATUS_MASK; }
        uint GET_RUN_STATUS(uint S) { return S & MTS_ST_RUNSTATUS_MASK; }

        void SET_LINK_STATUS(ref uint S, uint V) { S = (GET_LOAD_STATUS(S) | GET_RUN_STATUS(S) | V); }
        void SET_LOAD_STATUS(ref uint S, uint V) { S = (GET_LINK_STATUS(S) | GET_RUN_STATUS(S) | V); }
        void SET_RUN_STATUS(ref uint S, uint V) { S = (GET_LINK_STATUS(S) | GET_LOAD_STATUS(S) | V); }

        public mtRunEnv(mtRegisterInfo info, bool bAutoLogon)
        {
            m_info = info;
            m_info.SetAutoLogon(bAutoLogon);

            m_status = MTS_ST_IDLE | MTS_ST_CONNECTING | MTS_ST_UNINITIALIZE;
        }
        public emProgramType ProgramType { get => m_info.ProgramType; }

        public mtLogOnInfo LogonInfo { get => new mtLogOnInfo(m_info); }

        public mtRegisterInfo RegisterInfo { get => m_info; }

        public mtCredential Credential { get => m_crd; set => m_crd = value; }



        public bool IsLogin { get => m_crd.IsValid; }

        public void ClearCredential()
        {
            m_crd = null;
        }


        public bool IsAutoLogon { get => m_info.IsAutoLogon; }

        public bool IsAutoConnect { get => m_info.IsAutoConnect; }

        public void SetAutoLogon(bool b) { m_info.SetAutoLogon(b); }


        public uint RunStatus { get => m_status; set => m_status = value; }


        public void SetLinkStatus(uint nStatus)
        {
            SET_LINK_STATUS(ref m_status, nStatus);
        }

        public void SetUserNameAndPassword(string userName, string password)
        {
            m_info.UserName = userName;
            m_info.Password = password;
        }


        public void UpdateGStationRunStatus(mtGStationRunStatusInfo info)
        {
            m_localGStationRunStatusInfo = info;
        }

        public void UpdateGSystemRunStatus(mtGSystemStatus info)
        {
            m_GSystemStatus = info;

        }

        public mtGSystemStatus GetGSystemRunStatus()
        {
            return m_GSystemStatus;
        }

        public void UpdateGWorkFlowTask(mtGWFTask wfTask)
        {
            m_GWFTask = wfTask;
        }

        public void UpdateGWFTaskProcessInfo(mtGWFTaskProcessInfo info)
        {

        }

    }
}
